from math import log10
class ngram_score(object):
    def __init__(self,ngramfile):
      
        self.filekey = {}
        for line in file(ngramfile):
            key,count = line.split(" ")
            self.filekey[key] = float(count)
        self.length = len(key)
        self.sum1 = sum(self.filekey.itervalues())
        #calculate log probabilities
        for key in self.filekey.keys():
            self.filekey[key] = log10(float(self.filekey[key])/self.sum1)
        self.lowscore = log10(0.01/self.sum1)

    def score(self,message):
        ''' compute the score of text '''
        score = 0.0
       
        message = message.replace(" ","")
        message = message.replace(",","")
        message = message.replace(".","")
        message = message.replace("?","")
        message = message.replace("!","")
        ##print message
        
        ##print self.filekey
        for position in range(len(message)-self.length+1):           
            if message[position:position+self.length].upper() in self.filekey: 
                ##print self.filekey[message[position:position+self.length].upper()]
                score += float(self.filekey[message[position:position+self.length].upper()])
            else: 
                score += self.lowscore  
                ##print 'haha'
        return score